package com.database.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.database.common.BaseResultCodeEnum;
import com.database.common.ControllerErrorProcessor;
import com.database.common.result.CommonResult;
import com.database.common.result.ListResult;
import com.database.common.result.SingleResult;
import com.database.model.Food;
import com.database.model.Menu;
import com.database.model.Order;
import com.database.model.Shop;
import com.database.model.ShopKeeper;
import com.database.model.Staff;
import com.database.service.ShopKeeperService;

@Controller
@RequestMapping("shopKeeper/")
public class ShopKeeperController {
	@Autowired
	private ShopKeeperService shopKeeperService;
	Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 店家注册
	 * 
	 * @param shopKeeper
	 * @return
	 */
	@RequestMapping(value = "registeShopKeeper", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult registeShopKeeper(@RequestBody ShopKeeper shopKeeper) {
		try {
			int value = shopKeeperService.registeShopKeeper(shopKeeper);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店家登陆接口
	 * 
	 * @param shopKeeper
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public CommonResult login(@RequestBody ShopKeeper shopKeeper) {
		try {
			ShopKeeper value = shopKeeperService.login(shopKeeper);
			SingleResult<ShopKeeper> result = new SingleResult<ShopKeeper>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 返回店主信息
	 * 
	 * @param shopKeeper
	 * @return
	 */
	@RequestMapping(value = "findShopKeeperInfo", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public CommonResult findShopKeeperInfo(@RequestBody ShopKeeper shopKeeper) {
		try {
			ShopKeeper value = shopKeeperService.findShopKeeperInfo(shopKeeper);
			SingleResult<ShopKeeper> result = new SingleResult<ShopKeeper>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 更新店主个人信息
	 * @param shopKeeper
	 * @return
	 */
	@RequestMapping(value = "updateShopKeeperInfo", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public CommonResult updateShopKeeperInfo(@RequestBody ShopKeeper shopKeeper) {
		try {
			int value = shopKeeperService.updateShopKeeperInfo(shopKeeper);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 注册店铺
	 * 
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "registeShop", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult registeShop(@RequestBody Shop shop) {
		try {
			int value = shopKeeperService.registeShop(shop);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店家查看店铺列表
	 * 
	 * @param shopKeeper
	 * @return
	 */
	@RequestMapping(value = "findShopList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findShopList(@RequestBody Shop shop) {
		try {
			List<Shop> value = shopKeeperService.findShopList(shop);
			ListResult<Shop> result = new ListResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 查看单个店铺
	 * 
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "findShop", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findShop(@RequestBody Shop shop) {
		try {
			Shop value = shopKeeperService.findShop(shop);
			SingleResult<Shop> result = new SingleResult<Shop>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主更新店铺信息
	 * 
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "updateShopInfo", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updateShopInfo(@RequestBody Shop shop) {
		try {
			Integer value = shopKeeperService.updateShopInfo(shop);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 添加配送员
	 * 
	 * @param shop
	 * @return
	 */
	@RequestMapping(value = "addStaff", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addStaff(@RequestBody Staff staff) {
		try {
			Integer value = shopKeeperService.addStaff(staff);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主更新配送员店铺信息
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "updataStaffInfo", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updataStaffInfo(@RequestBody Staff staff) {
		try {
			System.err.println(staff.getName());
			Integer value = shopKeeperService.updataStaffInfo(staff);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主删除配送员
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "deleteStaff", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteStaff(@RequestBody Staff staff) {
		try {
			Integer value = shopKeeperService.deleteStaff(staff);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 查看配送员列表
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "findStaffList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findStaffList(@RequestBody Staff staff) {
		try {
			List<Staff> value = shopKeeperService.findStaffList(staff);
			ListResult<Staff> result = new ListResult<Staff>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 添加菜式
	 * 
	 * @param food
	 * @return
	 */
	@RequestMapping(value = "addFood", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addFood(@RequestBody Food food) {
		try {
			Integer value = shopKeeperService.addFood(food);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主更新菜式信息
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "updataFood", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updataFood(@RequestBody Food food) {
		try {
			Integer value = shopKeeperService.updataFood(food);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主删除菜式
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "deleteFood", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult deleteFood(@RequestBody Food food) {
		try {
			Integer value = shopKeeperService.deleteFood(food);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 查看店铺菜式列表
	 * 
	 * @param staff
	 * @return
	 */
	@RequestMapping(value = "findFoodList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findFoodList(@RequestBody Food food) {
		try {
			List<Food> value = shopKeeperService.findFoodList(food);
			ListResult<Food> result = new ListResult<Food>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店主查看单个菜式的信息
	 * 
	 * @param food
	 * @return
	 */
	@RequestMapping(value = "findFood", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findFood(@RequestBody Food food) {
		try {
			Food value = shopKeeperService.findFood(food);
			SingleResult<Food> result = new SingleResult<Food>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 查看店铺的订单
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "findOrderList", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findOrderList(@RequestBody Order order) {
		try {
			List<Order> value = shopKeeperService.findOrderList(order);
			ListResult<Order> result = new ListResult<Order>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 更改订单状态
	 * 
	 * @param food
	 * @return
	 */
	@RequestMapping(value = "updataOrderStatus", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updataOrderStatus(@RequestBody Order order) {
		try {
			Integer value = shopKeeperService.updataOrderStatus(order);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 店家查看单个订单
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "findOrder", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult findOrder(@RequestBody Menu menu) {
		try {
			List<Menu> value = shopKeeperService.findOrder(menu);
			ListResult<Menu> result = new ListResult<Menu>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}

	/**
	 * 为订单分配配送员
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "updataOrderStaff", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult updataOrderStaff(@RequestBody Order order) {
		try {
			Integer value = shopKeeperService.updataOrderStaff(order);
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, value);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return ControllerErrorProcessor.exceptionProcessor(ex, logger);
		}
	}
}
